package com.foreknow.service.impl;

import com.foreknow.bean.Guest;
import com.foreknow.dao.impl.GuestDaoImpl;
import com.foreknow.db.DBManager;
import com.foreknow.service.GuestService;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class GuestServiceImpl implements GuestService {
    @Override
    public Guest getQueryById(Integer id) {
        DBManager dbManager = DBManager.getInstance();
        Connection conn = dbManager.getConnection();
        GuestDaoImpl guestDao = new GuestDaoImpl();
        guestDao.setConnection(conn);
        return guestDao.getQueryById(id);
    }

    @Override
    public boolean update(Guest guest) {
        DBManager dbManager = DBManager.getInstance();
        Connection conn = dbManager.getConnection();
        GuestDaoImpl guestDao = new GuestDaoImpl();
        guestDao.setConnection(conn);
        boolean isRight = false;
        try {
            isRight = guestDao.update(guest);
            conn.commit();
        } catch (SQLException e) {
            e.printStackTrace();
            try {
                conn.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }
        return isRight;
    }

    @Override
    public List<Object> getQueryAll() {
        DBManager dbManager = DBManager.getInstance();
        Connection conn = dbManager.getConnection();
        GuestDaoImpl guestDao = new GuestDaoImpl();
        guestDao.setConnection(conn);
        return guestDao.getQueryAll();
    }

    @Override
    public boolean save(Guest guest) {
        DBManager dbManager = DBManager.getInstance();
        Connection conn = dbManager.getConnection();
        GuestDaoImpl guestDao = new GuestDaoImpl();
        guestDao.setConnection(conn);
        boolean isRigth = false;
        try {
            isRigth = guestDao.add(guest);
            conn.commit();
        } catch (SQLException e) {
            try {
                conn.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
            e.printStackTrace();
        }
        return isRigth;
    }
}
